package ing.gzq.secyrity;


import com.alibaba.fastjson.JSON;
import ing.gzq.base.ResultCache;
import ing.gzq.model.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by gzq on 17-6-11.
 */
@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication auth) throws IOException, ServletException {
        User user = (User) auth.getPrincipal();
        resp.setContentType("application/json; charset=utf-8");
        Cookie id = new Cookie("username",user.getUsername());
        Cookie name = new Cookie("name",user.getName());
        Cookie type = new Cookie("type",user.getType());
        resp.addCookie(id);
        resp.addCookie(name);
        resp.addCookie(type);
        resp.getWriter().write(JSON.toJSONString(ResultCache.OK));
    }
}
